package com.cy.mall.service.enter.impl;

import com.cy.mall.comm.utils.CommonUtils;
import com.cy.mall.comm.utils.EnterReportConstant;
import com.cy.mall.comm.utils.JsonObjectMapper;
import com.cy.mall.dao.mapper.enter.*;
import com.cy.mall.model.PageBean;
import com.cy.mall.model.ao.enter.*;
import com.cy.mall.model.domain.enter.*;
import com.cy.mall.model.dto.enter.*;
import com.cy.mall.model.dto.shuidai.*;
import com.cy.mall.model.query.enter.EnterListQuery;
import com.cy.mall.model.vo.enter.*;
import com.cy.mall.service.BaseService;
import com.cy.mall.service.enter.EnterService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.util.*;

/**
 * @Description 企业 service
 * @Author tanyilin
 * @Date 2019/3/20 14:01
 */
@Slf4j
@Service
public class EnterServiceImpl extends BaseService implements EnterService {

    @Autowired
    private EnterMapper enterMapper;
    @Autowired
    private EnterBaseInfoMapper enterBaseInfoMapper;
    @Autowired
    private EnterCorporateProfitMapper enterCorporateProfitMapper;
    @Autowired
    private EnterFinanceInfoMapper enterFinanceInfoMapper;
    @Autowired
    private EnterEvaluateRecordMapper enterEvaluateRecordMapper;
    @Autowired
    private EnterShareholderInfoMapper enterShareholderInfoMapper;
    @Autowired
    private EnterManagerInfoMapper enterManagerInfoMapper;
    @Autowired
    private EnterInvestmentInfoMapper enterInvestmentInfoMapper;
    @Autowired
    private EnterDeclareDataDetailMapper enterDeclareDataDetailMapper;

    @Override
    @Transactional
    public void addEnter(Enter enter) {

        Enter dbEnter = new Enter();
        dbEnter.setEnterCreditCode(enter.getEnterCreditCode());
        dbEnter = enterMapper.selectOne(dbEnter);
        state(dbEnter == null, "企业代码已存在");

        Date time = new Date();
        enter.setCreateTime(time);
        enter.setUpdateTime(time);
        enterMapper.insertSelective(enter);
    }

    @Override
    @Transactional
    public void updateEnter(Enter enter) {
        Date time = new Date();
        enter.setUpdateTime(time);
        enterMapper.updateByPrimaryKeySelective(enter);
    }

    @Override
    public Enter queryEnterDetail(Long enterId) {

        Enter enter = enterMapper.selectByPrimaryKey(enterId);
        return enter;
    }

    @Override
    public PageBean<EnterListVO> queryEnterList(EnterListQuery query) {

        int total = enterMapper.selectEnterListCount(query);
        List<EnterListVO> list = new ArrayList<>();

        if(total > 0) {
            query.startPage();
            list = enterMapper.selectEnterList(query);
        }

        return new PageBean<>(list, total);
    }

    @Override
    public EnterEvaluateStatusVO queryEvaluateStatus(Long enterId) {

        EnterEvaluateStatusVO statusVO = enterMapper.selectEnterEvaluateStatus(enterId);

//        //查询基本信息是否填写
//        EnterBaseInfo enterBaseInfo = new EnterBaseInfo();
//        enterBaseInfo.setEnterId(enterId);
//        enterBaseInfo = enterBaseInfoMapper.selectOne(enterBaseInfo);
//        if(enterBaseInfo != null) {
//            statusVO.setBaseInfo(true);
//        }
//
//        //查询股东信息是否填写
//        EnterShareholderInfo shareholderInfo = new EnterShareholderInfo();
//        shareholderInfo.setEnterId(enterId);
//        List<EnterShareholderInfo> shareholderInfoList = enterShareholderInfoMapper.select(shareholderInfo);
//        if(!CommonUtils.isEmpty(shareholderInfoList)) {
//            statusVO.setShareholderInfo(true);
//        }
//
//        //查询管理人员信息是否填写
//        EnterManagerInfo managerInfo = new EnterManagerInfo();
//        managerInfo.setEnterId(enterId);
//        List<EnterManagerInfo> managerInfoList = enterManagerInfoMapper.select(managerInfo);
//        if(!CommonUtils.isEmpty(managerInfoList)) {
//            statusVO.setManagerInfo(true);
//        }
//
//        //查询对外投资信息是否填写
//        EnterInvestmentInfo investmentInfo = new EnterInvestmentInfo();
//        investmentInfo.setEnterId(enterId);
//        List<EnterInvestmentInfo> investmentInfoList = enterInvestmentInfoMapper.select(investmentInfo);
//        if(!CommonUtils.isEmpty(investmentInfoList)) {
//            statusVO.setInvestmentInfo(true);
//        }
//
//        //查询申报数据明细是否填写
//        EnterDeclareDataDetail declareDataDetail = new EnterDeclareDataDetail();
//        declareDataDetail.setEnterId(enterId);
//        List<EnterDeclareDataDetail> declareDataDetailList = enterDeclareDataDetailMapper.select(declareDataDetail);
//        if(!CommonUtils.isEmpty(declareDataDetailList)) {
//            statusVO.setDeclareDataDetail(true);
//        }
//
//        //查询财务报表是否填写
//        EnterFinanceInfo enterFinanceInfo = new EnterFinanceInfo();
//        enterFinanceInfo.setEnterId(enterId);
//        List<EnterFinanceInfo> enterFinanceInfoList = enterFinanceInfoMapper.select(enterFinanceInfo);
//        if(!CommonUtils.isEmpty(enterFinanceInfoList)) {
//            statusVO.setFinanceInfo(true);
//        }
//
//        //查询利润表是否填写
//        EnterCorporateProfit enterCorporateProfit = new EnterCorporateProfit();
//        enterCorporateProfit.setEnterId(enterId);
//        List<EnterCorporateProfit> enterCorporateProfitList = enterCorporateProfitMapper.select(enterCorporateProfit);
//        if(!CommonUtils.isEmpty(enterCorporateProfitList)) {
//            statusVO.setCorporateProfit(true);
//        }

        return statusVO;
    }

    @Override
    public EnterBaseInfoDetailVO getEnterBaseInfo(Long enterId) {

        EnterBaseInfoDetailVO enterBaseInfoDetailVO = new EnterBaseInfoDetailVO();

        EnterBaseInfo enterBaseInfo = new EnterBaseInfo();
        enterBaseInfo.setEnterId(enterId);
        enterBaseInfo = enterBaseInfoMapper.selectOne(enterBaseInfo);

        if(enterBaseInfo != null) {
            BeanUtils.copyProperties(enterBaseInfo, enterBaseInfoDetailVO);
        }

        return enterBaseInfoDetailVO;
    }

    @Override
    @Transactional
    public void doSaveEnterBaseInfo(EnterBaseInfoSaveAO saveAO) {

        Date time = new Date();
        Long enterId = saveAO.getEnterId();

        //得到企业
        Enter dbEnter = enterMapper.selectByPrimaryKey(enterId);

        //根据 enterId 查询企业基本信息，若存在则更新，不存在则新增
        EnterBaseInfo dbEnterBaseInfo = new EnterBaseInfo();
        dbEnterBaseInfo.setEnterId(enterId);
        dbEnterBaseInfo = enterBaseInfoMapper.selectOne(dbEnterBaseInfo);

        if(dbEnterBaseInfo == null) {
            //若不存在则新增
            EnterBaseInfo newEnterBaseInfo = new EnterBaseInfo();
            BeanUtils.copyProperties(saveAO, newEnterBaseInfo);
            newEnterBaseInfo.setFddbrmc(dbEnter.getJuridicalName());
            newEnterBaseInfo.setFrYddhhm(dbEnter.getJuridicalPhone());
            newEnterBaseInfo.setFrZjhm(dbEnter.getJuridicalIdentityCard());
            newEnterBaseInfo.setCreateTime(time);
            newEnterBaseInfo.setUpdateTime(time);
            enterBaseInfoMapper.insertSelective(newEnterBaseInfo);

            //同时更新‘基本信息填写字段’为‘已填写’
            Enter updateEnter = new Enter();
            updateEnter.setId(enterId);
            updateEnter.setBaseInfo(true);
            enterMapper.updateByPrimaryKeySelective(updateEnter);

        }else {
            //若存在则更新
            BeanUtils.copyProperties(saveAO, dbEnterBaseInfo);
            dbEnterBaseInfo.setUpdateTime(time);
            enterBaseInfoMapper.updateByPrimaryKeySelective(dbEnterBaseInfo);
        }

    }

    @Override
    public List<EnterShareholderInfoVO> getEnterShareholderInfo(Long enterId) {
        return enterShareholderInfoMapper.selectEnterShareholderInfo(enterId);
    }

    @Override
    @Transactional
    public void doSaveShareholderInfo(EnterShareholderInfoSaveAO saveAO) {

        Date time = new Date();
        Long enterId = saveAO.getEnterId();
        List<EnterShareholderInfoListSaveDTO> shareholderInfoList = saveAO.getShareholderInfoList();

        //先删除
        enterShareholderInfoMapper.deleteAllByEnterId(enterId);

        //再新增
        for(EnterShareholderInfoListSaveDTO saveDTO : shareholderInfoList) {
            EnterShareholderInfo newEnterShareholderInfo = new EnterShareholderInfo();
            BeanUtils.copyProperties(saveDTO, newEnterShareholderInfo);
            newEnterShareholderInfo.setEnterId(enterId);
            newEnterShareholderInfo.setCreateTime(time);
            newEnterShareholderInfo.setUpdateTime(time);
            enterShareholderInfoMapper.insertSelective(newEnterShareholderInfo);
        }

        //同时更新‘股东信息填写字段’为‘已填写’
        Enter updateEnter = new Enter();
        updateEnter.setId(enterId);
        updateEnter.setShareholderInfo(true);
        enterMapper.updateByPrimaryKeySelective(updateEnter);

    }

    @Override
    public List<EnterManagerInfoVO> getEnterManagerInfo(Long enterId) {
        return enterManagerInfoMapper.selectEnterManagerInfo(enterId);
    }

    @Override
    @Transactional
    public void doSaveEnterManagerInfo(EnterManagerInfoSaveAO saveAO) {

        Date time = new Date();
        Long enterId = saveAO.getEnterId();
        List<EnterManagerInfoListSaveDTO> managerInfoList = saveAO.getManagerInfoList();

        //先删除
        enterManagerInfoMapper.deleteAllByEnterId(enterId);

        //再保存
        for(EnterManagerInfoListSaveDTO saveDTO : managerInfoList) {
            EnterManagerInfo newEnterManagerInfo = new EnterManagerInfo();
            BeanUtils.copyProperties(saveDTO, newEnterManagerInfo);
            newEnterManagerInfo.setEnterId(enterId);
            newEnterManagerInfo.setCreateTime(time);
            newEnterManagerInfo.setUpdateTime(time);
            enterManagerInfoMapper.insertSelective(newEnterManagerInfo);
        }

        //同时更新‘管理人员信息填写字段’为‘已填写’
        Enter updateEnter = new Enter();
        updateEnter.setId(enterId);
        updateEnter.setManagerInfo(true);
        enterMapper.updateByPrimaryKeySelective(updateEnter);

    }

    @Override
    public List<EnterInvestmentInfoVO> getEnterInvestmentInfo(Long enterId) {
        return enterInvestmentInfoMapper.selectEnterInvestmentInfo(enterId);
    }

    @Override
    @Transactional
    public void doSaveEnterInvestmentInfo(EnterInvestmentInfoSaveAO saveAO) {

        Date time = new Date();
        Long enterId = saveAO.getEnterId();
        List<EnterInvestmentInfoListSaveDTO> investmentInfoList = saveAO.getInvestmentInfoList();

        //先删除
        enterInvestmentInfoMapper.deleteAllByEnterId(enterId);

        //再保存
        for(EnterInvestmentInfoListSaveDTO saveDTO : investmentInfoList) {
            EnterInvestmentInfo newEnterInvestmentInfo = new EnterInvestmentInfo();
            BeanUtils.copyProperties(saveDTO, newEnterInvestmentInfo);
            newEnterInvestmentInfo.setEnterId(enterId);
            newEnterInvestmentInfo.setCreateTime(time);
            newEnterInvestmentInfo.setUpdateTime(time);
            enterInvestmentInfoMapper.insertSelective(newEnterInvestmentInfo);
        }

        //同时更新‘对外投资信息填写字段’为‘已填写’
        Enter updateEnter = new Enter();
        updateEnter.setId(enterId);
        updateEnter.setInvestmentInfo(true);
        enterMapper.updateByPrimaryKeySelective(updateEnter);

    }

    @Override
    public List<EnterDeclareDataDetailVO> getEnterDeclareDataDetail(Long enterId) {

        List<EnterDeclareDataDetailVO> list = enterDeclareDataDetailMapper.selectEnterDeclareDataDetail(enterId);

        if(CommonUtils.isEmpty(list)) {
            //如果第一次查询为空，则构造一个数组
            list = initEnterDeclareDataDetailList();
        }

        boolean flag = false;
        for(EnterDeclareDataDetailVO bean : list) {
            if(bean.getYear().equals(2019) && bean.getJidu().equals(2)) {
                flag = true;
            }
        }
        if(!flag) {
            //2019年2季度： 2019-04-01 至 2019-06-30
            EnterDeclareDataDetailVO bean20192 = new EnterDeclareDataDetailVO();
            bean20192.setZsxm("增值税");
            bean20192.setQbxssr("0");
            bean20192.setYsxssr("0");
            bean20192.setYnse("0");
            bean20192.setYjse("0");
            bean20192.setYbtse("0");
            bean20192.setJmse("0");
            bean20192.setYear(2019);
            bean20192.setJidu(2);
            bean20192.setSssqq("2019-04-01");
            bean20192.setSssqz("2019-06-30");
            list.add(bean20192);
        }

        return list;
    }

    @Override
    @Transactional
    public void doSaveEnterDeclareDataDetail(EnterDeclareDataDetailSaveAO saveAO) {

        Date time = new Date();
        Long enterId = saveAO.getEnterId();
        List<EnterDeclareDataDetailListSaveDTO> declareDataDetailList = saveAO.getDeclareDataDetailList();

        //先删除
        enterDeclareDataDetailMapper.deleteAllByEnterId(enterId);

        //再保存
        for(EnterDeclareDataDetailListSaveDTO saveDTO : declareDataDetailList) {
            EnterDeclareDataDetail newEnterDeclareDataDetail = new EnterDeclareDataDetail();
            BeanUtils.copyProperties(saveDTO, newEnterDeclareDataDetail);
            newEnterDeclareDataDetail.setEnterId(enterId);
            newEnterDeclareDataDetail.setCreateTime(time);
            newEnterDeclareDataDetail.setUpdateTime(time);
            enterDeclareDataDetailMapper.insertSelective(newEnterDeclareDataDetail);
        }

        //同时更新‘申报数据明细填写字段’为‘已填写’
        Enter updateEnter = new Enter();
        updateEnter.setId(enterId);
        updateEnter.setDeclareDataDetail(true);
        enterMapper.updateByPrimaryKeySelective(updateEnter);

    }

    @Override
    public Map<String, Object> getEnterCorporateProfit(Long enterId) {
        Map<String, Object> resultMap = new HashMap<>();

        List<Integer> recent3YearList = CommonUtils.getRecent3Year();
        List<EnterCorporateProfit> enterCorporateProfitList = enterCorporateProfitMapper.selectEnterCorporateProfitByEnterId(enterId, recent3YearList);
        if(!CommonUtils.isEmpty(enterCorporateProfitList)) {
            //如果利润报表不为空
            for(EnterCorporateProfit bean : enterCorporateProfitList) {
                EnterCorporateProfitDTO enterCorporateProfitDTO = new EnterCorporateProfitDTO();
                BeanUtils.copyProperties(bean, enterCorporateProfitDTO);
                LinkedHashMap<String, Object> map = new LinkedHashMap<>();
                try {
                    map = CommonUtils.bean2Map(enterCorporateProfitDTO);
                } catch (Exception e) {
                    log.error("enterCorporateProfitDTO 利用反射转 map 异常", e);
                }
                resultMap.put(bean.getReportYear().toString(), map);
            }
            //检验是否最近三年的数据都已存在
            for(Integer year : recent3YearList) {
                boolean yearIsExit = false;
                for(EnterCorporateProfit bean : enterCorporateProfitList) {
                    if(year.equals(bean.getReportYear())) {
                        yearIsExit = true;
                    }
                }
                if(!yearIsExit) {
                    //如果某个年份的数据表不存在，则初始化一个
                    EnterCorporateProfitDTO enterCorporateProfitDTO = new EnterCorporateProfitDTO();
                    LinkedHashMap<String, Object> map = initDefaultMap(enterCorporateProfitDTO);
                    resultMap.put(String.valueOf(year), map);
                }
            }

        }else {
            //如果 利润报表 is null
            //构造 初始化报表
            EnterCorporateProfitDTO enterCorporateProfitDTO = new EnterCorporateProfitDTO();
            LinkedHashMap<String, Object> map = initDefaultMap(enterCorporateProfitDTO);
            for(Integer year : recent3YearList) {
                resultMap.put(String.valueOf(year), map);
            }
        }

        return resultMap;
    }

    @Override
    @Transactional
    public void doSaveEnterCorporateProfit(EnterCorporateProfitSaveAO saveAO) {

        Date time = new Date();
        Long enterId = saveAO.getEnterId();
        List<EnterCorporateProfitListSaveDTO> corporateProfitList = saveAO.getCorporateProfitList();
        //先全部删除
        enterCorporateProfitMapper.deleteAllByEnterId(enterId);

        //再重新保存
        for(EnterCorporateProfitListSaveDTO saveDTO : corporateProfitList) {
            EnterCorporateProfit newEnterCorporateProfit = new EnterCorporateProfit();
            BeanUtils.copyProperties(saveDTO, newEnterCorporateProfit);
            newEnterCorporateProfit.setEnterId(enterId);
            newEnterCorporateProfit.setCreateTime(time);
            newEnterCorporateProfit.setUpdateTime(time);
            enterCorporateProfitMapper.insertSelective(newEnterCorporateProfit);
        }

        //同时更新‘利润表填写字段’为‘已填写’
        Enter updateEnter = new Enter();
        updateEnter.setId(enterId);
        updateEnter.setCorporateProfit(true);
        enterMapper.updateByPrimaryKeySelective(updateEnter);

    }

    @Override
    public Map<String, Object> getEnterFinanceInfo(Long enterId) {
        Map<String, Object> resultMap = new HashMap<>();

        List<Integer> recent3YearList = CommonUtils.getRecent3Year();
        List<EnterFinanceInfo> enterFinanceInfoList = enterFinanceInfoMapper.selectEnterFinanceInfoByEnterId(enterId, recent3YearList);
        if(!CommonUtils.isEmpty(enterFinanceInfoList)) {
            //如果 财务报表 不为空
            for(EnterFinanceInfo bean : enterFinanceInfoList) {
                EnterFinanceInfoDTO enterFinanceInfoDTO = new EnterFinanceInfoDTO();
                BeanUtils.copyProperties(bean, enterFinanceInfoDTO);
                LinkedHashMap<String, Object> map = new LinkedHashMap<>();
                try {
                    map = CommonUtils.bean2Map(enterFinanceInfoDTO);
                } catch (Exception e) {
                    log.error("enterFinanceInfoDTO 利用反射转 map 异常", e);
                }
                resultMap.put(bean.getReportYear().toString(), map);
            }

            //检验是否最近三年的数据都已存在
            for(Integer year : recent3YearList) {
                boolean yearIsExit = false;
                for(EnterFinanceInfo bean : enterFinanceInfoList) {
                    if(year.equals(bean.getReportYear())) {
                        yearIsExit = true;
                    }
                }
                if(!yearIsExit) {
                    //如果某个年份的数据表不存在，则初始化一个
                    EnterFinanceInfoDTO enterFinanceInfoDTO = new EnterFinanceInfoDTO();
                    LinkedHashMap<String, Object> map = initDefaultMap(enterFinanceInfoDTO);
                    resultMap.put(String.valueOf(year), map);
                }
            }

        }else {
            //如果 财务报表 is null
            //构造 初始化报表
            EnterFinanceInfoDTO enterFinanceInfoDTO = new EnterFinanceInfoDTO();
            LinkedHashMap<String, Object> map = initDefaultMap(enterFinanceInfoDTO);
            for(Integer year : recent3YearList) {
                resultMap.put(String.valueOf(year), map);
            }
        }

        return resultMap;
    }

    @Override
    @Transactional
    public void doSaveEnterFinanceInfo(EnterFinanceInfoSaveAO saveAO) {

        Date time = new Date();
        Long enterId = saveAO.getEnterId();
        List<EnterFinanceInfoListSaveDTO> financeInfoList = saveAO.getFinanceInfoList();

        //先全部删除
        enterFinanceInfoMapper.deleteAllByEnterId(enterId);

        //再重新保存
        for(EnterFinanceInfoListSaveDTO saveDTO : financeInfoList) {
            EnterFinanceInfo newEnterFinanceInfo = new EnterFinanceInfo();
            BeanUtils.copyProperties(saveDTO, newEnterFinanceInfo);
            newEnterFinanceInfo.setEnterId(enterId);
            newEnterFinanceInfo.setCreateTime(time);
            newEnterFinanceInfo.setUpdateTime(time);
            enterFinanceInfoMapper.insertSelective(newEnterFinanceInfo);
        }

        //同时更新‘财务表填写字段’为‘已填写’
        Enter updateEnter = new Enter();
        updateEnter.setId(enterId);
        updateEnter.setFinanceInfo(true);
        enterMapper.updateByPrimaryKeySelective(updateEnter);

    }

    @Override
    public Map<String, Object> getEnterEvaluateRequestParam(Long enterId) {

        //查询企业基本信息
        DbEnterBaseInfoRequestDTO dbEnterBaseInfo = enterBaseInfoMapper.selectEnterBaseInfoRequestDTO(enterId);
        EnterBaseInfoRequestDTO enterBaseInfo = new EnterBaseInfoRequestDTO();
        BeanUtils.copyProperties(dbEnterBaseInfo, enterBaseInfo);
        enterBaseInfo.setZCZB_ZE(dbEnterBaseInfo.getZCZBZE());
        enterBaseInfo.setNSRLX_MC(dbEnterBaseInfo.getNSRLXMC());
        enterBaseInfo.setZCD_DHHM(dbEnterBaseInfo.getZCDDHHM());
        enterBaseInfo.setHYMX_DM(dbEnterBaseInfo.getHYMXDM());
        enterBaseInfo.setHYMX_MC(dbEnterBaseInfo.getHYMXMC());
        enterBaseInfo.setFR_ZJHM(dbEnterBaseInfo.getFRZJHM());
        enterBaseInfo.setFR_YDDHHM(dbEnterBaseInfo.getFRYDDHHM());
        enterBaseInfo.setNSRZT_MC(dbEnterBaseInfo.getNSRZTMC());

//        //查询股东信息列表
//        List<EnterShareholderInfoRequestDTO> shareholderInfoList = enterShareholderInfoMapper.selectEnterShareholderInfoRequestDTO(enterId);
//        //查询管理人员信息
//        List<EnterManagerInfoRequestDTO> managerInfoList = enterManagerInfoMapper.selectEnterManagerInfoRequestDTO(enterId);
//        //查询对外投资信息
//        List<EnterInvestmentInfoRequestDTO> investmentInfoList = enterInvestmentInfoMapper.selectEnterInvestmentInfoRequestDTO(enterId);

        //查询申报数据明细
        List<EnterDeclareDataDetailRequestDTO> declareDataDetailList = enterDeclareDataDetailMapper.selectEnterDeclareDataDetailRequestDTO(enterId);
        if(!CommonUtils.isEmpty(declareDataDetailList)) {
            declareDataDetailList.stream().forEach(d -> {
                d.setQBXSSR(new BigDecimal(d.getQBXSSR()).multiply(new BigDecimal(10000)).setScale(2).toString());
                d.setYSXSSR(new BigDecimal(d.getYSXSSR()).multiply(new BigDecimal(10000)).setScale(2).toString());
                d.setYNSE(new BigDecimal(d.getYNSE()).multiply(new BigDecimal(10000)).setScale(2).toString());
                d.setYJSE(new BigDecimal(d.getYJSE()).multiply(new BigDecimal(10000)).setScale(2).toString());
                d.setYBTSE(new BigDecimal(d.getYBTSE()).multiply(new BigDecimal(10000)).setScale(2).toString());
                d.setJMSE(new BigDecimal(d.getJMSE()).multiply(new BigDecimal(10000)).setScale(2).toString());
            });
        }
        Map<String, Object> SBXX_LIST = new HashMap<>();
        SBXX_LIST.put("SBXX", declareDataDetailList);

        //查询企业财务报表信息
        //Map<String, Object> financeInfo = new HashMap<>();
        List<Map<String, String>> ZCFZB = new ArrayList<>();
        List<EnterFinanceInfo> enterFinanceInfoList = enterFinanceInfoMapper.selectEnterFinanceInfoByEnterId(enterId, CommonUtils.getRecent3Year());
        if(!CommonUtils.isEmpty(enterFinanceInfoList)) {
            //如果 财务报表 不为空
            for(EnterFinanceInfo bean : enterFinanceInfoList) {
                EnterFinanceInfoDTO enterFinanceInfoDTO = new EnterFinanceInfoDTO();
                BeanUtils.copyProperties(bean, enterFinanceInfoDTO);
                LinkedHashMap<String, String> map = new LinkedHashMap<>();
                try {
                    map = CommonUtils.bean2MapUpperKey(enterFinanceInfoDTO);
                } catch (Exception e) {
                    log.error("enterFinanceInfoDTO 利用反射转 map 异常", e);
                }
                map.remove("YUANJIA");
                map.remove("ZHEJIU");
                map.put("yuanjia", String.valueOf(enterFinanceInfoDTO.getYuanjia().multiply(new BigDecimal(10000)).setScale(2)));
                map.put("zhejiu", String.valueOf(enterFinanceInfoDTO.getZhejiu().multiply(new BigDecimal(10000)).setScale(2)));
                //检查key的大小写
                //financeInfo.put(bean.getReportYear().toString(), map);

                String sssqq = String.format("%s-01-01", bean.getReportYear());
                String sssqz = String.format("%s-12-31", bean.getReportYear());
                for(String key : map.keySet()) {
                    Map<String, String> zcfzbMap = new HashMap<>();
                    zcfzbMap.put("SSSQQ", sssqq);
                    zcfzbMap.put("SSSQZ", sssqz);
                    zcfzbMap.put("XM", EnterReportConstant.getMc(key));
                    zcfzbMap.put("QMYE", map.get(key));
                    zcfzbMap.put("BBLX", "2");
                    ZCFZB.add(zcfzbMap);
                }
            }
        }
        Map<String, Object> ZCFZB_LIST = new HashMap<>();
        ZCFZB_LIST.put("ZCFZB", ZCFZB);

        //查询企业利润报表信息
        //Map<String, Object> corporateProfit = new HashMap<>();
        List<Map<String, String>> LRBXX = new ArrayList<>();
        List<EnterCorporateProfit> enterCorporateProfitList = enterCorporateProfitMapper.selectEnterCorporateProfitByEnterId(enterId, CommonUtils.getRecent3Year());
        if(!CommonUtils.isEmpty(enterCorporateProfitList)) {
            //如果利润报表不为空
            for(EnterCorporateProfit bean : enterCorporateProfitList) {
                EnterCorporateProfitDTO enterCorporateProfitDTO = new EnterCorporateProfitDTO();
                BeanUtils.copyProperties(bean, enterCorporateProfitDTO);
                LinkedHashMap<String, String> map = new LinkedHashMap<>();
                try {
                    map = CommonUtils.bean2MapUpperKey(enterCorporateProfitDTO);
                } catch (Exception e) {
                    log.error("enterCorporateProfitDTO 利用反射转 map 异常", e);
                }
                // 检查key的大小写
                //corporateProfit.put(bean.getReportYear().toString(), map);

                String sssqq = String.format("%s-01-01", bean.getReportYear());
                String sssqz = String.format("%s-12-31", bean.getReportYear());
                for(String key : map.keySet()) {
                    Map<String, String> lrbxxMap = new HashMap<>();
                    lrbxxMap.put("SSSQQ", sssqq);
                    lrbxxMap.put("SSSQZ", sssqz);
                    lrbxxMap.put("XM", EnterReportConstant.getMc(key));
                    lrbxxMap.put("BQJE", map.get(key));
                    lrbxxMap.put("BBLX", "2");
                    LRBXX.add(lrbxxMap);
                }
            }
        }
        Map<String, Object> LRBXX_LIST = new HashMap<>();
        LRBXX_LIST.put("LRBXX", LRBXX);

        Map<String, Object> TZFXX_LIST = new HashMap<>();
        TZFXX_LIST.put("TZFXX", new ArrayList<>());

//        EnterEvaluateRequestParamDTO requestParamDTO = new EnterEvaluateRequestParamDTO();
//        requestParamDTO.setEnterBaseInfo(enterBaseInfo);
//        requestParamDTO.setShareholderInfoList(shareholderInfoList);
//        requestParamDTO.setManagerInfoList(managerInfoList);
//        requestParamDTO.setInvestmentInfoList(investmentInfoList);
//        requestParamDTO.setDeclareDataDetailList(declareDataDetailList);
//        requestParamDTO.setFinanceInfo(financeInfo);
//        requestParamDTO.setCorporateProfit(corporateProfit);

        EnterEvaluateRequestParamDTO SYWZBG = new EnterEvaluateRequestParamDTO();
        SYWZBG.setJCXX(enterBaseInfo);
        SYWZBG.setSBXX_LIST(SBXX_LIST);
        SYWZBG.setZCFZB_LIST(ZCFZB_LIST);
        SYWZBG.setLRBXX_LIST(LRBXX_LIST);
        SYWZBG.setTZFXX_LIST(TZFXX_LIST);

        Map<String, Object> RISK_INFO = new HashMap<>();
        RISK_INFO.put("CORE-LABEL3", dbEnterBaseInfo.getLastEvaluateAmount());
        RISK_INFO.put("CORE-LABEL1", dbEnterBaseInfo.getEnterName());
        RISK_INFO.put("ZMXYF", dbEnterBaseInfo.getMYZMF());

        Map<String, Object> requestParamMap = new HashMap<>();
        requestParamMap.put("RISK-INFO", RISK_INFO);
        requestParamMap.put("SYWZBG", SYWZBG);

        return requestParamMap;
    }

    @Override
    @Transactional
    public void doSaveEvaluateRecord(Map<String, Object> resultMap) {

        Date time = new Date();
        String bizId = String.valueOf(resultMap.get("bizId"));
        Long enterId = Long.valueOf(String.valueOf(resultMap.get("enterId")));
        BigDecimal amount = resultMap.get("amount") == null ? BigDecimal.ZERO : new BigDecimal(String.valueOf(resultMap.get("amount")));

        //保存评估记录
        EnterEvaluateRecord newEnterEvaluateRecord = new EnterEvaluateRecord();
        newEnterEvaluateRecord.setBizNo(bizId);
        newEnterEvaluateRecord.setEnterId(enterId);
        newEnterEvaluateRecord.setAmount(amount);
        newEnterEvaluateRecord.setCreateTime(time);
        newEnterEvaluateRecord.setUpdateTime(time);
        enterEvaluateRecordMapper.insertSelective(newEnterEvaluateRecord);

        //更新企业最后一次评估额度、时间,以及评估次数
        Enter dbEnter = enterMapper.selectByPrimaryKey(enterId);
        Enter updateEnter = new Enter();
        updateEnter.setId(enterId);
        updateEnter.setEvaluateCount(dbEnter.getEvaluateCount() + 1);
        updateEnter.setLastEvaluateAmount(amount);
        updateEnter.setLastEvaluateTime(time);
        enterMapper.updateByPrimaryKeySelective(updateEnter);

    }

    /**
     * 构造初始化 企业-申报数据明细 数组
     * @param
     * @return java.util.List<com.cy.mall.model.vo.enter.EnterDeclareDataDetailVO>
     */
    private List<EnterDeclareDataDetailVO> initEnterDeclareDataDetailList() {
        List<EnterDeclareDataDetailVO> list = new ArrayList<>();

        EnterDeclareDataDetailVO bean = new EnterDeclareDataDetailVO();
        bean.setZsxm("增值税");
        bean.setQbxssr("0");
        bean.setYsxssr("0");
        bean.setYnse("0");
        bean.setYjse("0");
        bean.setYbtse("0");
        bean.setJmse("0");
        //2017年1季度： 2017-01-01 至 2017-03-31
        EnterDeclareDataDetailVO bean20171 = new EnterDeclareDataDetailVO();
        BeanUtils.copyProperties(bean, bean20171);
        bean20171.setYear(2017);
        bean20171.setJidu(1);
        bean20171.setSssqq("2017-01-01");
        bean20171.setSssqz("2017-03-31");
        list.add(bean20171);
        //2017年2季度： 2017-04-01 至 2017-06-30
        EnterDeclareDataDetailVO bean20172 = new EnterDeclareDataDetailVO();
        BeanUtils.copyProperties(bean, bean20172);
        bean20172.setYear(2017);
        bean20172.setJidu(2);
        bean20172.setSssqq("2017-04-01");
        bean20172.setSssqz("2017-06-30");
        list.add(bean20172);
        //2017年3季度： 2017-07-01 至 2017-09-30
        EnterDeclareDataDetailVO bean20173 = new EnterDeclareDataDetailVO();
        BeanUtils.copyProperties(bean, bean20173);
        bean20173.setYear(2017);
        bean20173.setJidu(3);
        bean20173.setSssqq("2017-07-01");
        bean20173.setSssqz("2017-09-30");
        list.add(bean20173);
        //2017年4季度： 2017-10-01 至 2017-12-31
        EnterDeclareDataDetailVO bean20174 = new EnterDeclareDataDetailVO();
        BeanUtils.copyProperties(bean, bean20174);
        bean20174.setYear(2017);
        bean20174.setJidu(4);
        bean20174.setSssqq("2017-10-01");
        bean20174.setSssqz("2017-12-31");
        list.add(bean20174);

        //2018年1季度： 2018-01-01 至 2018-03-31
        EnterDeclareDataDetailVO bean20181 = new EnterDeclareDataDetailVO();
        BeanUtils.copyProperties(bean, bean20181);
        bean20181.setYear(2018);
        bean20181.setJidu(1);
        bean20181.setSssqq("2018-01-01");
        bean20181.setSssqz("2018-03-31");
        list.add(bean20181);
        //2018年2季度： 2018-04-01 至 2018-06-30
        EnterDeclareDataDetailVO bean20182 = new EnterDeclareDataDetailVO();
        BeanUtils.copyProperties(bean, bean20182);
        bean20182.setYear(2018);
        bean20182.setJidu(2);
        bean20182.setSssqq("2018-04-01");
        bean20182.setSssqz("2018-06-30");
        list.add(bean20182);
        //2018年3季度： 2018-07-01 至 2018-09-30
        EnterDeclareDataDetailVO bean20183 = new EnterDeclareDataDetailVO();
        BeanUtils.copyProperties(bean, bean20183);
        bean20183.setYear(2018);
        bean20183.setJidu(3);
        bean20183.setSssqq("2018-07-01");
        bean20183.setSssqz("2018-09-30");
        list.add(bean20183);
        //2018年4季度： 2018-10-01 至 2018-12-31
        EnterDeclareDataDetailVO bean20184 = new EnterDeclareDataDetailVO();
        BeanUtils.copyProperties(bean, bean20184);
        bean20184.setYear(2018);
        bean20184.setJidu(4);
        bean20184.setSssqq("2018-10-01");
        bean20184.setSssqz("2018-12-31");
        list.add(bean20184);

        //2019年1季度： 2019-01-01 至 2019-03-31
        EnterDeclareDataDetailVO bean20191 = new EnterDeclareDataDetailVO();
        BeanUtils.copyProperties(bean, bean20191);
        bean20191.setYear(2019);
        bean20191.setJidu(1);
        bean20191.setSssqq("2019-01-01");
        bean20191.setSssqz("2019-03-31");
        list.add(bean20191);
        //2019年2季度： 2019-04-01 至 2019-06-30
        EnterDeclareDataDetailVO bean20192 = new EnterDeclareDataDetailVO();
        BeanUtils.copyProperties(bean, bean20192);
        bean20192.setYear(2019);
        bean20192.setJidu(2);
        bean20192.setSssqq("2019-04-01");
        bean20192.setSssqz("2019-06-30");
        list.add(bean20192);

        return list;
    }

    /**
     * 构造初始化 map
     * @param beanObj
     * @return java.util.LinkedHashMap<java.lang.String,java.lang.Object>
     */
    private LinkedHashMap<String, Object> initDefaultMap(Object beanObj) {
        if(beanObj == null){
            return null;
        }
        LinkedHashMap<String, Object> map = new LinkedHashMap<>();
        //获取所有的属性
        Field[] fields = beanObj.getClass().getDeclaredFields();
        for(Field field : fields) {
            //打破封装，设置可访问
            field.setAccessible(true);
            map.put(field.getName(), 0);
        }
        return map;
    }


}
